﻿using System;
using System.Linq;
using System.Linq.Expressions;

namespace QuakeStats
{
    class ItemEntry : LogEntry
    {
        public ItemEntry(string line) : base(line)
        {}

        protected override void ParseLineData(string line)
        {
            //  1:57 Item: 2 ammo_rockets
            string values = line.Trim().Split(':')[2].Trim();
            ClientId = values.Split(' ')[0].Trim();
            ItemDescription = values.Split(' ')[1].Trim();
            ItemId = int.Parse(values.Split(' ')[0].Trim());

            InitDbItem();
        }

        private void InitDbItem()
        {
            DbItem  = SingleContext.Instance.Items.Where(i => i.Description == ItemDescription).FirstOrDefault();
            if (DbItem == null)
            {
                DbItem = new Item();
                DbItem.Description = ItemDescription;
                DbItem.ItemId = ItemId;
                SingleContext.Instance.AddToItems(DbItem);
                SingleContext.Instance.SaveChanges();
            }
        }

        public Item DbItem { get; set; }
        public int ItemId { get; set; }
        public string ItemDescription { get; set; }
        public string ClientId { get; set; }
    }
}
